import { FlyFootManager } from "../../manager/FlyFootManager";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { Game } from "../../Game";
import { Cache } from "../../cache/Cache";
import { MapFileUtil } from "../../scene/map/MapFileUtil";
import { GameMapUtil } from "../../scene/map/GameMapUtil";
import { TaskTargetInfo } from "../../model/TaskTargetInfo";
import { EntityType } from "../../rules/EntityType";
import { AIManager } from "../../scene/ai/AIManager";
import { AIType } from "../../scene/ai/AIType";
import { ItemLayer } from "./ItemLayer";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { AStarPoint } from "../../scene/map/AStarPoint";
import { PathLineDraw } from "./PathLineDraw";
import { SEntityInfo } from "../../../../Message/BroadCast/SEntityInfo";
import { EntityUtil } from "../../scene/layer/utils/EntityUtil";
import { ObjectPool } from "../../../../com/gengine/utils/pools/ObjectPool";
import { EntityPoint } from "./point/EntityPoint";
import { SEntityId } from "../../../../Message/Public/SEntityId";
import { SMapAllEntityPoint } from "../../../../Message/BroadCast/SMapAllEntityPoint";
import { RobCityUtill } from "../robCity/RobCityUtill";
import { EntityMapPoint } from "./point/EntityMapPoint";
import { KingChampionShipUtil } from "../copy/kingChampionShip/KingChampionShipUtil";
import { EForce } from "../../../../Message/Public/EForce";
import { EntityAttachedSoulBattlefieldPoint } from "./point/EntityAttachedSoulBattlefieldPoint";
import { AttachedSoulBattlefieldUtil } from "../attachedSoulBattlefield/AttachedSoulBattlefieldUtil";
import { EntityRobFlagMapPoint } from "./point/EntityRobFlagMapPoint";
import { ERelationType } from "../../../../Message/Public/ERelationType";
import { SBossPoints } from "../../../../Message/Public/SBossPoints";
type int = number;
//class CurrentMap
    
    export  class CurrentMap /* flash.display.Sprite */
    {
        constructor()
        {
            
            mouseEnabled = false;
            this.initUI();
            return;
        }

        public clearTarget(): void
        {
            this._itemLayer.cleanTarget();
            return;
        }

        public clearFlyFoot(): void
        {
            FlyFootManager.hideFlyFoot();
            return;
        }

        public clearSceneUnit(): void
        {
            this._itemLayer.clean();
            return;
        }

        public clearBossPoint(): void
        {
            this._itemLayer.cleanBossPoint();
            return;
        }

        protected onPicLoad(arg1: ImageInfo): void
        {
            if (!arg1 || !arg1.bitmapData) 
            {
                return;
            }
            mouseEnabled = true;
            mouseChildren = true;
            this._mapBitmap.bitmapData = arg1.bitmapData;
            var loc1=(this.W - 0) / this._mapBitmap.width;
            var loc2=(this.H - 0) / this._mapBitmap.height;
            var loc3=Math.min(loc1, loc2);
            this._mapBitmap.width = this._mapBitmap.width * loc3;
            this._mapBitmap.height = this._mapBitmap.height * loc3;
            this._scaleWidth = this._mapBitmap.width / Game.mapInfo.gridWidth;
            this._scaleHeight = this._mapBitmap.height / Game.mapInfo.gridHeight;
            var loc4;
            this._mapBody.x = loc4 = (this.W - this._mapBitmap.width) / 2 + 0;
            this._drawLayer.x = loc4 = loc4;
            this._itemLayer.x = loc4;
            this._mapBody.y = loc4 = (this.H - this._mapBitmap.height) / 2 + 0;
            this._drawLayer.y = loc4 = loc4;
            this._itemLayer.y = loc4;
            this.updatePoint(Cache.instance.mapui.currentPosition);
            this.updatePath(Cache.instance.mapui.mapFindPath);
            this.updateSceneUnit();
            if (Cache.instance.huntTask.isShowLocationTarget(MapFileUtil.mapID)) 
            {
                this.targetLocation(MapFileUtil.mapID, Cache.instance.huntTask.huntTaskInfo.px, Cache.instance.huntTask.huntTaskInfo.py);
            }
            return;
        }

        protected onPicBgLoad(arg1: ImageInfo): void
        {
            if (arg1 && arg1.bitmapData) 
            {
                this._mapBg.bitmapData = arg1.bitmapData;
                this._mapBg.width = this.W;
                this._mapBg.height = this.H;
            }
            return;
        }

        /* internal  */onCurrentMapClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            if (this._mapBitmap.bitmapData != null) 
            {
                loc1 = GameMapUtil.localToTilePoint(arg1.localX - 2, arg1.localY - 2, 1 / this._scaleWidth, 1 / this._scaleHeight);
                loc2 = new TaskTargetInfo();
                loc2.x = loc1.x;
                loc2.y = loc1.y;
                loc2.mapId = MapFileUtil.mapID;
                loc2.targetType = EntityType.Point;
                loc2.proxyId = MapFileUtil.proxyID;
                loc2.serverId = MapFileUtil.serverID;
                AIManager.onAutoPathAIControl(loc2, AIType.AI_Scene);
                loc3 = GameMapUtil.tilePointToLocal(loc1.x, loc1.y, this._scaleWidth, this._scaleHeight);
                FlyFootManager.showFlyFoot(this, this._itemLayer.x + loc3.x - 20, this._itemLayer.y + loc3.y - 20, loc1.x, loc1.y, MapFileUtil.mapID, 0, EntityType.Point, loc2.proxyId, loc2.serverId);
            }
            return;
        }

        public updateSceneUnit(): void
        {
            this._itemLayer.updateSceneUnit(this._scaleWidth, this._scaleHeight, Game.sceneInfo);
            return;
        }

        public updateNpc(): void
        {
            this._itemLayer.cleanNPC();
            this.updateSceneUnit();
            return;
        }

        public targetLocation(arg1: int, arg2: int, arg3: int): void
        {
            if (arg1 == MapFileUtil.mapID) 
            {
                this._itemLayer.targetLocation(arg1, arg2, arg3, this._scaleWidth, this._scaleHeight);
            }
            return;
        }

        protected initUI(): void
        {
            this._mapBg = new Object /* flash.display.Bitmap */();
            addChild(this._mapBg);
            this._mapBody = new Object /* flash.display.Sprite */();
            this._mapBitmap = new Object /* flash.display.Bitmap */();
            this._drawLayer = new Object /* flash.display.Sprite */();
            this._itemLayer = new ItemLayer(true);
            this._drawLayer.mouseEnabled = false;
            this._itemLayer.mouseEnabled = false;
            this._mapBody.addChild(this._mapBitmap);
            addChild(this._mapBody);
            addChild(this._drawLayer);
            addChild(this._itemLayer);
            this._mapBody.addEventListener(flash.events.MouseEvent.CLICK, this.onCurrentMapClick, false, 0, true);
            this._entityMap = new Map<any, any> /* flash.utils.Dictionary */();
            LoaderManager.instance.load("smallmapbg.jpg", this.onPicBgLoad);
            return;
        }

        public updateMap(arg1: int): void
        {
            this.mouseEnabled = false;
            this.mouseChildren = false;
            this.clearLine();
            this.clearSceneUnit();
            LoaderManager.instance.load(MapFileUtil.getMiniMapPathByMap(arg1), this.onPicLoad);
            return;
        }

        public updatePoint(arg1: AStarPoint=null): void
        {
            if (arg1 == null || Game.sceneInfo == null) 
            {
                return;
            }
            if (this._mapBitmap.bitmapData == null) 
            {
                return;
            }
            var loc1=GameMapUtil.tilePointToLocal(arg1.x, arg1.y, this._scaleWidth, this._scaleHeight);
            this._itemLayer.setRolePosition(loc1.x, loc1.y);
            return;
        }

        public updatePath(arg1: Array<any>): void
        {
            if (arg1 == null || arg1.length == 0) 
            {
                this.clearLine();
                return;
            }
            if (this._mapBitmap.bitmapData == null) 
            {
                return;
            }
            PathLineDraw.draw(this._drawLayer, arg1, Cache.instance.mapui.currentPosition, this._scaleWidth, this._scaleHeight);
            var loc1=arg1[(arg1.length - 1)];
            var loc2=GameMapUtil.tilePointToLocal(loc1.x, loc1.y, this._scaleWidth, this._scaleHeight);
            this._itemLayer.setTargetPosition(loc2.x, loc2.y);
            return;
        }

        public initEntity(): void
        {
            var loc1=null;
            this.clearEntity();
            var loc2=0;
            var loc3=Cache.instance.mapui.mapEntity;
            for(loc1 of loc3) 
            {
                this.entityEnter(loc1);
            }
            return;
        }

        public entityEnter(arg1: SEntityInfo): void
        {
            if (this._entityMap.hasOwnProperty(EntityUtil.toString(arg1.entityId))) 
            {
                this.entityLeft(arg1.entityId);
            }
            var loc1=ObjectPool.getObject(EntityPoint);
            loc1.mouseChildren = true;
            loc1.mouseEnabled = true;
            loc1.updateData(arg1);
            this._itemLayer.addChildAt(loc1, 0);
            this._entityMap[EntityUtil.toString(arg1.entityId)] = loc1;
            var loc2=arg1.points[0];
            var loc3=GameMapUtil.tilePointToLocal(loc2.x, loc2.y, this._scaleWidth, this._scaleHeight);
            loc1.updatePosition(loc3.x, loc3.y);
            return;
        }

        public entityLeft(arg1: SEntityId): void
        {
            var loc1=this._entityMap[EntityUtil.toString(arg1)];
            if (loc1 != null) 
            {
                delete this._entityMap[EntityUtil.toString(arg1)];
                this._itemLayer.removeChild(loc1);
                loc1.dispose();
                ObjectPool.disposeObject(loc1, EntityPoint);
            }
            return;
        }

        public entityUpdate(arg1: SEntityInfo): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=this._entityMap[EntityUtil.toString(arg1.entityId)];
            if (loc1 != null) 
            {
                if ((loc4 = Cache.instance.mapui.mapEntity[EntityUtil.toString(loc1.entityId)]) != null) 
                {
                    loc3 = loc4.points[0];
                    loc2 = GameMapUtil.tilePointToLocal(loc3.x, loc3.y, this._scaleWidth, this._scaleHeight);
                    loc1.updatePosition(loc2.x, loc2.y);
                }
            }
            return;
        }

        public removeLocation(): void
        {
            this._itemLayer.removeLocation();
            return;
        }

        public allEntityPointUpdate(arg1: SMapAllEntityPoint): void
        {
            var loc1=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc8=0;
            var loc9=0;
            this.disposeAllEntityPoint();
            var loc2=0;
            while (loc2 < arg1.entityPoints.length) 
            {
                loc1 = arg1.entityPoints[loc2];
                if (loc1.entityId.id != Cache.instance.role.entityInfo.entityId.id) 
                {
                    if (GameMapUtil.curMapState.isRobCityCopyTwo || GameMapUtil.curMapState.isRobCityCopyThree) 
                    {
                        loc6 = RobCityUtill.getForceColor(loc1.relationType, true);
                        (loc3 = ObjectPool.getObject(EntityMapPoint)).updateData(null, loc6, 2, 1);
                        loc4 = loc1.point;
                        loc5 = GameMapUtil.tilePointToLocal(loc4.x, loc4.y, this._scaleWidth, this._scaleHeight);
                        loc3.updatePosition(loc5.x, loc5.y);
                        this._itemLayer.addChildAt(loc3, 0);
                        this._aryPoint.push(loc3);
                    }
                    else if (GameMapUtil.curMapState.isKingChampionShip) 
                    {
                        loc7 = KingChampionShipUtil.getForceColor(loc1.relationType, true);
                        (loc3 = ObjectPool.getObject(EntityMapPoint)).updateData(null, loc7, 3, 1);
                        loc4 = loc1.point;
                        loc5 = GameMapUtil.tilePointToLocal(loc4.x, loc4.y, this._scaleWidth, this._scaleHeight);
                        loc3.updatePosition(loc5.x, loc5.y);
                        this._itemLayer.addChildAt(loc3, 0);
                        this._aryPoint.push(loc3);
                    }
                    else if (GameMapUtil.curMapState.isCrossSpiritWarCopyCode) 
                    {
                        if (loc1.relationType == EForce._EForceThree || loc1.relationType == EForce._EForceFour) 
                        {
                            (loc3 = ObjectPool.getObject(EntityAttachedSoulBattlefieldPoint)).updateData(loc1);
                        }
                        else 
                        {
                            loc8 = AttachedSoulBattlefieldUtil.getForceColor(loc1.relationType, true);
                            (loc3 = ObjectPool.getObject(EntityAttachedSoulBattlefieldPoint)).updateData(null, loc8, 4.5, 1);
                            EntityAttachedSoulBattlefieldPoint(loc3).setUpdateObj(loc1);
                        }
                        loc4 = loc1.point;
                        loc5 = GameMapUtil.tilePointToLocal(loc4.x, loc4.y, this._scaleWidth, this._scaleHeight);
                        loc3.updatePosition(loc5.x, loc5.y);
                        this._itemLayer.addChildAt(loc3, 0);
                        this._aryPoint.push(loc3);
                    }
                    else if (GameMapUtil.curMapState.isRobFlag) 
                    {
                        if (!(loc1.name == null) && !(loc1.name == "")) 
                        {
                            loc3 = ObjectPool.getObject(EntityMapPoint);
                            loc9 = 65280;
                            if (loc1.relationType != EForce._EForceOne) 
                            {
                                loc9 = 41727;
                            }
                            else 
                            {
                                loc9 = 16711680;
                            }
                            loc3.updateData(null, loc9, 4, 1);
                        }
                        else 
                        {
                            (loc3 = ObjectPool.getObject(EntityRobFlagMapPoint)).updateData(loc1);
                        }
                        loc4 = loc1.point;
                        loc5 = GameMapUtil.tilePointToLocal(loc4.x, loc4.y, this._scaleWidth, this._scaleHeight);
                        loc3.updatePosition(loc5.x, loc5.y);
                        this._itemLayer.addChildAt(loc3, 0);
                        this._aryPoint.push(loc3);
                    }
                    else if (GameMapUtil.curMapState.isGuildElite) 
                    {
                        (loc3 = ObjectPool.getObject(EntityMapPoint)).updateData(loc1);
                        loc4 = loc1.point;
                        loc5 = GameMapUtil.tilePointToLocal(loc4.x, loc4.y, this._scaleWidth, this._scaleHeight);
                        loc3.updatePosition(loc5.x, loc5.y);
                        this._itemLayer.addChildAt(loc3, 0);
                        this._aryPoint.push(loc3);
                    }
                    else if (loc1.relationType != ERelationType._ERelationFriend) 
                    {
                        (loc3 = ObjectPool.getObject(EntityMapPoint)).updateData(loc1);
                        loc4 = loc1.point;
                        loc5 = GameMapUtil.tilePointToLocal(loc4.x, loc4.y, this._scaleWidth, this._scaleHeight);
                        loc3.updatePosition(loc5.x, loc5.y);
                        this._itemLayer.addChildAt(loc3, 0);
                        this._aryPoint.push(loc3);
                    }
                }
                ++loc2;
            }
            return;
        }

        public disposeAllEntityPoint(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._aryPoint;
            for(loc1 of loc3) 
            {
                if (loc1.parent) 
                {
                    loc1.parent.removeChild(loc1);
                }
                loc1.dispose();
                if (typeof loc1 === "entityrobflagmappoint") 
                {
                    ObjectPool.disposeObject(loc1, EntityRobFlagMapPoint);
                    continue;
                }
                if (typeof loc1 === "entityattachedsoulbattlefieldpoint") 
                {
                    ObjectPool.disposeObject(loc1, EntityAttachedSoulBattlefieldPoint);
                    continue;
                }
                ObjectPool.disposeObject(loc1, EntityMapPoint);
            }
            this._aryPoint = [];
            return;
        }

        public bossPointUpdate(arg1: SBossPoints): void
        {
            this._itemLayer.updateBossPoint(arg1, this._scaleWidth, this._scaleHeight);
            return;
        }

        public updateBossRefreshPoint(arg1: Array<any>): void
        {
            this._itemLayer.updateBossRefreshPoint(arg1);
            return;
        }

        public updatePassPoint(): void
        {
            this._itemLayer.updatePassPoint();
            return;
        }

        public clearEntity(): void
        {
            var loc1=undefined;
            var loc2=null;
            var loc3=0;
            var loc4=this._entityMap;
            for (loc1 in loc4) 
            {
                loc2 = this._entityMap[loc1];
                delete this._entityMap[loc1];
                this._itemLayer.removeChild(loc2);
                loc2.dispose();
                if (typeof loc2 === "entityrobflagmappoint") 
                {
                    ObjectPool.disposeObject(loc2, EntityRobFlagMapPoint);
                    continue;
                }
                if (typeof loc2 === "entityattachedsoulbattlefieldpoint") 
                {
                    ObjectPool.disposeObject(loc2, EntityAttachedSoulBattlefieldPoint);
                    continue;
                }
                if (typeof loc2 === "entitymappoint") 
                {
                    ObjectPool.disposeObject(loc2, EntityMapPoint);
                    continue;
                }
                if (typeof !(loc2 === "entitypoint")) 
                {
                    continue;
                }
                ObjectPool.disposeObject(loc2);
            }
            return;
        }

        public clearLine(): void
        {
            this._drawLayer.graphics.clear();
            return;
        }

        /* internal */ /* const */W: int=586;

        /* internal */ /* const */H: int=350;

        protected /* var */_mapBg: Object /* flash.display.Bitmap */;

        protected /* var */_mapBody: Object /* flash.display.Sprite */;

        protected /* var */_itemLayer: ItemLayer;

        protected /* var */_drawLayer: Object /* flash.display.Sprite */;

        protected /* var */_mapBitmap: Object /* flash.display.Bitmap */;

        protected /* var */_scaleWidth: number=0;

        protected /* var */_scaleHeight: number=0;

        protected /* var */_entityMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_aryPoint: Array<any>;
    }
